package com.testfile.binfo.geteway;


import com.testfile.binfo.model.FileInfo;
import com.testfile.binfo.uitl.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class FileController {

    /**
     * 查询路径下所有文件
     * @param filePath
     * @return
     */
    @RequestMapping("/testGetFiles")
    @ResponseBody
    public List<FileInfo> testGetFiles(@RequestParam("filePath") String filePath){
        return FileUtil.getFiles(filePath);
    }

    /**
     * 下载文件
     * 读取的response为二进制形式，无法使用ajax接收执行
     * @param httpServletResponse
     * @param filePathName
     */
    @RequestMapping("/downLoadFile")
    @ResponseBody
    public void downLoadFile(HttpServletResponse httpServletResponse,@RequestParam("filePathName") String filePathName){
        FileUtil.downloadFile(httpServletResponse,filePathName);
    }

    /**
     * 文件上传
     * @param file
     * @param directPath
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file")MultipartFile file,@RequestParam("directPath") String directPath){
        return FileUtil.fileUpload(file,directPath);
    }
}
